//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Note Names Plugin
//
//  Copyright (C) 2012 Werner Schweer
//  Copyright (C) 2013, 2014 Joachim Schmitz
//  Copyright (C) 2014 Jörn Eichler
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
    version: "2.0"
    description: qsTr("This plugin names SimpleStaff")
    menuPath: "Plugins." + qsTr("SimpleStaff") // this does not work, why?

    function nameChord(notes, text, cursor) {
        var textDotC0 = newElement(Element.STAFF_TEXT);
        var textDotC1 = newElement(Element.STAFF_TEXT);
        var textDotC2 = newElement(Element.STAFF_TEXT);
        var textDotC3 = newElement(Element.STAFF_TEXT);
        var textDotC5 = newElement(Element.STAFF_TEXT);
        var textDotC6 = newElement(Element.STAFF_TEXT);
        var textDotC7 = newElement(Element.STAFF_TEXT);
        var textDotC8 = newElement(Element.STAFF_TEXT);
        
        textDotC0.fontSize = 8;
        textDotC1.fontSize = 8;
        textDotC2.fontSize = 8;
        textDotC3.fontSize = 8;
        textDotC5.fontSize = 8;
        textDotC6.fontSize = 8;
        textDotC7.fontSize = 8;
        textDotC8.fontSize = 8;

        for (var i = 0; i < notes.length; i++) {
            var sep = " "; // change to "\n" if you want them vertically
            if (i > 0)
                text.text = sep + text.text;

            if (typeof notes[i].tpc === "undefined") // just in case
                return
            if (notes[i].pitch < 24) {
                textDotC0.text = qsTr(".  ") + textDotC0.text
            }
            if (notes[i].pitch < 24 + 12) {
                textDotC1.text = qsTr(".  ") + textDotC1.text
            }
            if (notes[i].pitch < 24 + 12 * 2) {
                textDotC2.text = qsTr(".  ") + textDotC2.text
            }
            if (notes[i].pitch < 24 + 12 * 3) {
                textDotC3.text = qsTr(".  ") + textDotC3.text
            }
            

            if (notes[i].pitch >= 24 + 12 * 4) {
                textDotC5.text = qsTr(".  ") + textDotC5.text
            }
            if (notes[i].pitch >= 24 + 12 * 5) {
                textDotC6.text = qsTr(".  ") + textDotC6.text
            }
            if (notes[i].pitch >= 24 + 12 * 6) {
                textDotC7.text = qsTr(".  ") + textDotC7.text
            }
            if (notes[i].pitch >= 24 + 12 * 7) {
                textDotC8.text = qsTr(".  ") + textDotC8.text
            }
            switch (notes[i].tpc) {
                case -1: text.text = qsTr("4") + text.text; break;
                case 0: text.text = qsTr("1") + text.text; break;
                case 1: text.text = qsTr("5") + text.text; break;
                case 2: text.text = qsTr("2") + text.text; break;
                case 3: text.text = qsTr("6") + text.text; break;
                case 4: text.text = qsTr("3") + text.text; break;
                case 5: text.text = qsTr("7") + text.text; break;
                case 6: text.text = qsTr("4") + text.text; break;
                case 7: text.text = qsTr("1") + text.text; break;

                case 8: text.text = qsTr("5") + text.text; break;
                case 9: text.text = qsTr("2") + text.text; break;
                case 10: text.text = qsTr("6") + text.text; break;
                case 11: text.text = qsTr("3") + text.text; break;
                case 12: text.text = qsTr("7") + text.text; break;
                case 13: text.text = qsTr("4") + text.text; break;
                case 14: text.text = qsTr("1") + text.text; break;
                case 15: text.text = qsTr("5") + text.text; break;
                case 16: text.text = qsTr("2") + text.text; break;
                case 17: text.text = qsTr("6") + text.text; break;
                case 18: text.text = qsTr("3") + text.text; break;
                case 19: text.text = qsTr("7") + text.text; break;

                case 20: text.text = qsTr("4") + text.text; break;
                case 21: text.text = qsTr("1") + text.text; break;
                case 22: text.text = qsTr("5") + text.text; break;
                case 23: text.text = qsTr("2") + text.text; break;
                case 24: text.text = qsTr("6") + text.text; break;
                case 25: text.text = qsTr("3") + text.text; break;
                case 26: text.text = qsTr("7") + text.text; break;
                case 27: text.text = qsTr("4") + text.text; break;
                case 28: text.text = qsTr("1") + text.text; break;
                case 29: text.text = qsTr("5") + text.text; break;
                case 30: text.text = qsTr("2") + text.text; break;
                case 31: text.text = qsTr("6") + text.text; break;
                case 32: text.text = qsTr("3") + text.text; break;
                case 33: text.text = qsTr("7") + text.text; break;
                default: text.text = qsTr("?") + text.text; break;
            } // end switch tpc
            //console.log(notes[i].pitch)

            //console.log(notes[i].accidentalType)
            //for(var ss in notes[i]){       
            //	console.log(ss +  " :  " + eval("notes[i]." + ss))   
            //}  

            switch (String(notes[i].accidentalType)) {
                case "NONE":
                    text.offsetX =  0.65;
                    text.offsetY =  3;
                    break;
                case "SHARP":
                    text.text = qsTr("♯") + text.text;
                    break;
                case "FLAT":
                    text.text = qsTr("♭") + text.text;
                    break;
                case "SHARP2":
                    text.text = qsTr("♯♯") + text.text;
                    break;
                case "FLAT2":
                    text.text = qsTr("♭♭") + text.text;
                    break;
                case "NATURAL":
                    text.text = qsTr("♮") + text.text;
                    break;
                default:
                    text.text = qsTr("?") + text.text;
                    break;
            } // end switch userAccidental
            
        } // end for note
		
		
		text.offsetX = text.offsetX - (notes.length - 1) * 0.5;
        textDotC0.offsetX = textDotC0.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC1.offsetX = textDotC1.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC2.offsetX = textDotC2.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC3.offsetX = textDotC3.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC5.offsetX = textDotC5.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC6.offsetX = textDotC6.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC7.offsetX = textDotC7.offsetX + 0.95 - (notes.length - 1) * 0.5;
        textDotC8.offsetX = textDotC8.offsetX + 0.95 - (notes.length - 1) * 0.5;


        cursor.add(textDotC0);
        cursor.add(textDotC1);
        cursor.add(textDotC2);
        cursor.add(textDotC3);
        cursor.add(text);
        cursor.add(textDotC5);
        cursor.add(textDotC6);
        cursor.add(textDotC7);
        cursor.add(textDotC8);

    }

    onRun: {
        if (typeof curScore === 'undefined')
            Qt.quit();
        var cursor = curScore.newCursor();
        var style = curScore.style;
        //console.log(style.value("minVerticalDistance") + "ss")
        style.setValue("staffTextMinDistance", 0.2);
        //console.log(style.value("minVerticalDistance") + "ss")
        var startStaff;
        var endStaff;
        var endTick;
        var fullScore = false;
        cursor.rewind(1);
        if (!cursor.segment) { // no selection
            fullScore = true;
            startStaff = 0; // start with 1st staff
            endStaff = curScore.nstaves - 1; // and end with last
        } else {
            startStaff = cursor.staffIdx;
            cursor.rewind(2);
            if (cursor.tick == 0) {
                // this happens when the selection includes
                // the last measure of the score.
                // rewind(2) goes behind the last segment (where
                // there's none) and sets tick=0
                endTick = curScore.lastSegment.tick + 1;
            } else {
                endTick = cursor.tick;
            }
            endStaff = cursor.staffIdx;
        }
        console.log(startStaff + " - " + endStaff + " - " + endTick)

        for (var staff = startStaff; staff <= endStaff; staff++) {
            for (var voice = 0; voice < 4; voice++) {
                cursor.rewind(1); // beginning of selection
                cursor.voice = voice;
                cursor.staffIdx = staff;

                if (fullScore)  // no selection
                    cursor.rewind(0); // beginning of score

                while (cursor.segment && (fullScore || cursor.tick < endTick)) {
                    if (cursor.element && cursor.element.type == Element.CHORD) {
                        var text = newElement(Element.STAFF_TEXT);
                        var notes = cursor.element.notes;
                        text.fontSize = 8
                        switch (voice) {
                            case 0: text.offsetY = 1; break;
                            case 1: text.offsetY = 10; break;
                            case 2: text.offsetY = -1; break;
                            case 3: text.offsetY = 12; break;
                        }
                     
                        nameChord(notes, text, cursor);
                    } // end if CHORD
                    cursor.next();
                } // end while segment
            } // end for voice
        } // end for staff
        Qt.quit();
    } // end onRun
}
